package com.msb.servicemap.remote;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.msb.internalcommon.constant.AmapConfigConstants;
import com.msb.internalcommon.response.DirectionResponse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: WuWen
 * @Date: 2023/8/20 - 08 - 20 - 11:20
 * @Description: com.msb.servicemap.remote
 * @version: 1.0
 */
@Service
@Slf4j
public class MapDirectionClient {


    @Autowired
    RestTemplate restTemplate;

    @Value("${amap.key}")
    private String amapKey;

    public DirectionResponse mapDirection(String depLongitude, String depLatitude, String destLongitude, String destLatitude) {

        // 组装URL

        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.ROUTE_URL);
        url.append("?");
        url.append("origin=").append(depLongitude).append(",").append(depLatitude);
        url.append("&");
        url.append("destination=").append(destLongitude).append(",").append(destLatitude);
        url.append("&");
        url.append("extensions=base");
        url.append("&");
        url.append("output=json");
        url.append("&");
        url.append("key=").append(amapKey);

        String directorUrl = url.toString();


        // 调用地图接口
        ResponseEntity<String> directionEntity = restTemplate.getForEntity(directorUrl, String.class);
        String directionString = directionEntity.getBody();

        // log.info("返回的信息" + directionEntity);

        // 解析接口
        DirectionResponse directionResponse = parseDirectionEntity(directionString);

        return directionResponse;
    }

    public DirectionResponse parseDirectionEntity(String directionString) {
        DirectionResponse directionResponse = null;
        try {
            JSONObject result = JSONObject.parseObject(directionString);
            int status = result.getInteger(AmapConfigConstants.STATUS);
            if (status == 1) {
                if (result.containsKey((AmapConfigConstants.ROUTE))){
                    JSONObject routeObject = result.getObject(AmapConfigConstants.ROUTE, JSONObject.class);
                    JSONArray pathsArray = routeObject.getJSONArray(AmapConfigConstants.PATHS);
                    JSONObject pathObject = pathsArray.getJSONObject(0);
                    directionResponse = new DirectionResponse();

                    if (pathObject.containsKey(AmapConfigConstants.DISTANCE)){
                        Integer distance = pathObject.getInteger(AmapConfigConstants.DISTANCE);
                        directionResponse.setDistance(distance);
                    }

                    if (pathObject.containsKey(AmapConfigConstants.DURATION)){
                        Integer duration = pathObject.getInteger(AmapConfigConstants.DURATION);
                        directionResponse.setDuration(duration);
                    }

                }

            }

        } catch (Exception e) {

        }
        return directionResponse;
    }
}
